package cn.maihe.elg.operation.supports.electronicseal.kt;

public class KTAutoSealJavaClt
{
	static {
		if( System.getProperty("sun.arch.data.model").equals("32") )
			System.loadLibrary( "AutoSealJava_x86" );
		else
			System.loadLibrary( "AutoSealJava_x64" );
	}


	//分配空间
	public native long initilize_new( String strUrl, String strUser, String strPasswd );
	public native long initilize_new( String strUrl, String strUser, String strPasswd, boolean bSealColorPrintNotCtrl );
	public KTAutoSealJavaClt( String strUrl, String strUser, String strPasswd )
	{
		m_nHandle = 0;
		m_strLastError = "";
		m_nHandle = initilize_new( strUrl, strUser, strPasswd );
	}
	public KTAutoSealJavaClt( String strUrl, String strUser, String strPasswd, boolean bSealColorPrintNotCtrl )
	{
		m_nHandle = 0;
		m_strLastError = "";
		m_nHandle = initilize_new( strUrl, strUser, strPasswd, bSealColorPrintNotCtrl );
	}

	//添加单个印章模板(多个印章模板循环调用)
	public native boolean first_add_seal_on_paper( String strSealID, int nPage, int nX, int nY, boolean bOverText );
	public boolean First_add_seal_on_paper( String strSealID, int nPage, int nX, int nY, boolean bOverText )
	{
		return first_add_seal_on_paper(  strSealID, nPage,  nX , nY,  bOverText );
	}

	//添加单个印章模板以关键字查找pdf定位的形式(多个印章模板循环调用)
	/*************************************************************************
	 Function : first_add_seal_on_paper
	 Purpose  : 添加印章以关键字查找pdf定位
	 Params   : [in]  strSealID				- 印章id
	 [in]  byKeyword				- utf-8编码的字符串
	 [in]  bUnique
	 真(1)				- 关键字在pdf中有且只能有一处，否则调用报错
	 假(0)				- 关键字在pdf中如果有多处，则将签多个章
	 [in]  nXOffset				- pdf坐标系X坐标方向偏移：小于0向左，大于0向右
	 [in]  nYOffset				- pdf坐标系Y坐标方向偏移：小于0向上，大于0向下
	 [in]  bOverText				- 印章是否浮现于文字上方
	 Return   : 成功返回 真(1)，失败返回 假(0)
	 *************************************************************************/
	public native boolean first_add_seal_on_paper( String strSealID, byte[] byKeyword, boolean bUnique, int nXOffset, int nYOffset, boolean bOverText );

	//添加单个印章模板以关键字查找pdf定位的形式(多个印章模板循环调用)
	/*************************************************************************
	 Function : first_add_seal_on_paper
	 Purpose  : 添加印章以关键字查找pdf定位
	 Params   : [in]  strSealID				- 印章id
	 [in]  byKeyword			- utf-8编码的字符串
	 [in]  nPage			- 0开始代表第一页，N代表N+1页
	 [in]  nOrdinalNum   - 标识码
	 -2代表查找的关键字有且只能有一个；
	 -1代表查找文档中的所有关键字；
	 0代表查找某页nPage中的所有关键字；
	 >0代表某页nPage中的第几个关键字(序数查找形式从左到右、从上到下)
	 [in]  nXOffset				- pdf坐标系X坐标方向偏移：小于0向左，大于0向右
	 [in]  nYOffset				- pdf坐标系Y坐标方向偏移：小于0向上，大于0向下
	 [in]  bOverText    -- 印章是否浮现于文字上方
	 Return   : 成功返回 真(1)，失败返回 假(0)
	 *************************************************************************/
	public native boolean first_add_seal_on_paper( String strSealID, byte[] byKeyword, int nPage, int nOrdinalNum, int nXOffset, int nYOffset, boolean bOverText );

	//使用印章id对应的证书对批注进行签名签章（替换该id所对应的印章）
	//如果该批注为字符串，则长度限制为256字节（即128个字符）
	public native boolean first_add_remarks_replace_on_paper( String strSealID, String strRemarks, int nRemarkWidth, byte[] byKeyword, boolean bUnique, int nXOffset, int nYOffset, boolean bOverText );
	public native boolean first_add_remarks_replace_on_paper( String strSealID, String strRemarks, int nRemarkWidth, byte[] byKeyword, int nPage, int nOrdinalNum, int nXOffset, int nYOffset, boolean bOverText );
	public native boolean first_add_remarks_replace_on_paper( String strSealID, int nType, String strRemarks, int nRemarkWidth, int nRemarkHeight, byte[] byKeyword, int nPage, int nOrdinalNum, int nXOffset, int nYOffset, boolean bOverText );
	public native boolean first_add_remarks_replace_on_paper( String strSealID, String strRemarks, int nRemarkWidth, int nPage, int nX, int nY, boolean bOverText);

	//清空已添加的全部印章模板
	public native boolean seal_on_paper_clear();

	//添加单个底图模板或批注模板(多个底图模板可循环调用，底图全部大小最多不超过3M)
	public native boolean second_add_backpic_on_paper( int nType, String strPicPathOrID, int nPage, int nX, int nY, int nWidth, int nHeight, boolean bOverText );

	//添加批注模板，一般紧跟随于first_add_seal_on_paper方法之后调用，nPage值0代表第一页，以此类推
	public native boolean second_add_remarks_backpic_on_paper( int nType, String strSealID, String strRemarks, int nPage, int nX, int nY, int nWidth, int nHeight, boolean bOverText );
	public native boolean second_add_remarks_backpic_on_paper( int nType, String strSealID, String strRemarks, int nPage, int nX, int nY, int nWidth, int nHeight );
	public native boolean second_add_remarks_backpic_on_paper( int nType, String strSealID, String strRemarks, int nPage, int nX, int nY );

	//清空已添加的全部底图模板
	public native boolean backpic_on_paper_clear();

	//签章转换文档
	public native int final_convert_document( String strInFile, int nUniqueFlag, boolean bDoc2pdfOnly, String strOutFile );
	public   int Final_convert_document( String strInFile, int nUniqueFlag, boolean bDoc2pdfOnly, String strOutFile )
	{
		return final_convert_document( strInFile, nUniqueFlag, bDoc2pdfOnly, strOutFile );
	}

	//签章转换文档扩展方法
	public native int final_convert_document_ex( String strInFile, String strExt1, String strExt2, boolean bDoc2pdfOnly, String strOutFile );
	public native int final_convert_document_ex( String strInFile, String strExt1, String strExt2, boolean bDoc2pdfOnly, boolean bPdfFormatSignature, String strOutFile );

	//输入文件流签章转换文档
	public native int final_convert_document2( byte[] docBuffer, String strDocExtern,int nUniqueFlag, boolean bDoc2pdfOnly, String strOutFile );
	//输入文件流签章转换文档扩展方法
	public native int final_convert_document2_ex( byte[] docBuffer, String strDocExtern ,String strExt1, String strExt2, boolean bDoc2pdfOnly, String strOutFile );

	//输出文件流签章转换文档
	public native byte[] final_convert_document3( byte[] docBuffer, String strDocExtern,int nUniqueFlag, boolean bDoc2pdfOnly );
	//输出文件流签章转换文档扩展方法
	public native byte[] final_convert_document3_ex( byte[] docBuffer, String strDocExtern, String strExt1, String strExt2, boolean bDoc2pdfOnly );

	// 验证签章文件
	public native String direct_verify_document( String strInFile );

	//释放handle空间
	public native void finally_free();
	public native void finally_free_ex( long nHandle );
	public void KTFreeHandle()
	{
		finally_free_ex( m_nHandle );
	}

	//获取字符串描述
	public native String get_error_by_code( int nErrorCode );

	//获取上个方法调用失败的错误字符串描述
	public String get_last_error()
	{
		return m_strLastError;
	}

	private long m_nHandle;
	private String m_strLastError;

	private static void SealDemoTest()
	{
		KTAutoSealJavaClt clt = new KTAutoSealJavaClt( "125.77.73.161:2040", "test", "11111", true );


		if( !clt.first_add_seal_on_paper( "E9AD490B16CD65458FBC1BCF9783C5EA", 0, 200, 200, true ) )
		{
			System.out.println( clt.get_last_error() );
			clt.finally_free();
			return;
		}

        /*
		try
		{
	        String remarks = " ";
			//System.out.println( remarks );
			//String keyword = "案管签字";
			//boolean bRet = clt.first_add_seal_on_paper( "E9C37E0A37CA794F9BE8EB0E736189F5", 0, 200, 200, true );
			//boolean bRet = clt.first_add_seal_on_paper( "E9C37E0A37CA794F9BE8EB0E736189F5", 0, 200, 200, true );
			//boolean bRet = clt.first_add_seal_on_paper("AD47BD96BDE89A4A8CD4F2DC9A51A3DD", keyword.getBytes("utf-8"), true, 0, 0, true);
			//boolean bRet = clt.first_add_seal_on_paper("E9AD490B16CD65458FBC1BCF9783C5EA", keyword.getBytes("utf-8"), true, 0, 0, true);
			//boolean bRet = clt.first_add_seal_on_paper("56cf4221608f4dd7af0606d00a2e0f1d", keyword.getBytes("utf-8"), true, 0, 0, true);
			boolean bRet = clt.first_add_remarks_replace_on_paper("E9AD490B16CD65458FBC1BCF9783C5EA", remarks, 500, 0, 200, 200, true);
			//boolean bRet = clt.first_add_remarks_replace_on_paper("E9AD490B16CD65458FBC1BCF9783C5EA", remarks, 500, keyword.getBytes("utf-8"), false, 0, 0, true);
			if( !bRet )
			{
				System.out.println( clt.get_last_error() );
				clt.finally_free();
				return;
			}
		}
		catch(Exception e)
		{
			 System.out.println("this is a test exception:" + e);
			 clt.finally_free();
			 return;
		}
        */
		//int nRet= clt.final_convert_document("c:/1.pdf" ,0, false, "c:/1.edc"  );
		//int nRet= clt.final_convert_document("c:/1.edc" ,0, true, "c:/out.pdf"  );
		int nRet= clt.final_convert_document_ex("d:/home/担保保函模板.pdf" , null, null, false, true, "d:/home/担保保函模板_out.pdf"  );
		if( 0 != nRet )
		{
			System.out.println( nRet + "[" + clt.get_last_error() + "]" );
			clt.finally_free();
			return;
		}
		System.out.println( "success!!!" );

		clt.finally_free();

	}

	public static void main(String[] args)
	{
		SealDemoTest();
	}
}

